package mvc.control;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import mvc.model.Circle;

/*
 * esiste la classe MouseApter che implementa tutti i metodi di MouseListener e MouseMotionListener
 * con metodi vuoti, per non usarli
 * (faccio l'overrride solo di quelli che mi occorrono)
 * avremmo potuto utilizzarlo 
 */

public class CircleController implements MouseListener,MouseMotionListener{
	
	private Circle circle;
	private boolean dragging;
	
	private int dx;		// xMouse - xCerchio
	private int dy;		// yMouse - yCerchio
	
	public CircleController(Circle circle) {
		super();
		this.circle = circle;
	}

	@Override
	public void mousePressed(MouseEvent event) {
		int xMouse=event.getX();
		int yMouse=event.getY();
		
		int ray=circle.getRay();
		int x=circle.getX();
		int y=circle.getY();
		
		setDx(xMouse-circle.getX());
		setDy(yMouse-circle.getY());
		
		if( (xMouse-x)*(xMouse-x) + (yMouse-y)*(yMouse-y) <= ray*ray ){
			dragging=true;			
		}else{
			dragging=false;
		}
	}
	
	@Override
	public void mouseDragged(MouseEvent event) {		
		
		int xMouse=event.getX();
		int yMouse=event.getY();
		
		if(dragging){
			circle.setX(xMouse-getDx());
			circle.setY(yMouse-getDy());
		}
		
	}
	

	public int getDx() {
		return dx;
	}

	public void setDx(int dx) {
		this.dx = dx;
	}

	public int getDy() {
		return dy;
	}

	public void setDy(int dy) {
		this.dy = dy;
	}

	
	
	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseMoved(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

}
